#ifndef DBS_H
#define DBS_H

#include "inifile.h"
#include <string>
#include <vector>
#include <mysql_connection.h>

//数据库池化

class sql::Connection;
class db_pool
{
public:
    bool init(int nPools, const string& strServer, const string& strUid, const string& strPwd, const string& schema, apr_pool_t* pool);
    void destory();

    sql::Connection * GetDBC();
    void ReleaseDBC(sql::Connection*);

private:
    sql::Connection * init_DBC();

private:
    apr_queue_t* m_dbs;
    apr_pool_t*  m_pool;
    unsigned int m_nPools;
    string m_strUri;//etc. tcp://192.168.0.1:3306
    string m_strUid;
    string m_strPwd;
    string m_strSchema;
};

enum CALL_TYPE
{
    CALLBACK1 = 1,
    CALLBACK2 = 2,
    VOIP = 3,
    DIRECT = 4,
    GATEWAY = 5,
    CONFERENCE = 6,
	AUTHCALL=7
};
typedef struct
{
    //语音编码,透传彩铃标志，落地，最大时长，外呼主叫，外呼被叫
    string codec;
    int    bRingFlag;
    string server;
    int    nTimeout;
    string caller;
    string called;
} call_param_t;

//数据库池操作接口
bool initDBS(CIniFile& config, char* database, apr_pool_t* pool);
void destoryDBS();

bool getCallParam(const string& acc, const string& caller, const string& called, CALL_TYPE calltype, vector<call_param_t>& callParam);
bool bill(CALL_TYPE calltype, const char* acc, const char* caller, const char* called, const char* server, const char* startTime, const char* endTime, int nCallTime);


bool insertCallback(string& acct, string& caller, string& called);
//VIPpay
bool vippay(const string& acctname,int flag);
//查询是否存在用户
bool isExitUser(const string& acctname,string& idstr);
//绑定回拨亲亲号
bool bindcbnum(const string& acctname,string& bindnum,string& sipnum);
//查询回拨快捷号码
bool getcbnum(const string& acctname,string& sipnum,string& quicknum);
//绑定亲亲号
bool bindqinqin(const string& acctname,string& bindnum,string& sipnum);
//查询亲亲快捷号码
bool getquicknum(const string& acctname,string& sipnum,string& quicknum);
//绑定短号
bool bindshort(const string& acctname,string& bindnum);
//短号转计费号
bool shorttoacctname(const string& bindnum,string& acctname);
bool getbalance(const string& acc, const string& pwd, const string& caller, int type, double& fbalance);
bool charged(const string& caller, const string& acct, const string& card, const string& pwd, float& card_value, float& fbalance);
//预约回拨使用接口
sql::Connection* GetDBC();
void ReleaseDBC(sql::Connection*);

//认证用户
bool auth_voip(const string& acc, const string& caller,string& authcode);
bool insertAuth(string& acct, string& caller);

//base64编码
bool base64_encode(string& str);
bool base64_decode(string& str);


#endif // DBS_H
